package com.feijiang.service.impl;

import com.feijiang.entity.ExpeStep;
import com.feijiang.mapper.ExpeStepMapper;
import com.feijiang.mapper.ExperimentMapper;
import com.feijiang.service.IExpeStepService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.feijiang.service.IExperimentService;
import com.feijiang.util.MyPage;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2021-12-28
 */
@Service
public class ExpeStepServiceImpl extends ServiceImpl<ExpeStepMapper, ExpeStep> implements IExpeStepService {
  @Resource
  ExpeStepMapper expeStepMapper;

  @Autowired
  ExperimentMapper experimentMapper;

  @Override
  public boolean add(ExpeStep expeStep) {
    // 先判断实验信息是否存在
    Assert.notNull(experimentMapper.selectById(expeStep.getExpeId()), "实验信息的ID不存在");
    int i = expeStepMapper.insert(expeStep);
    Assert.isTrue(i>0, "插入失败");
    return true;
  }

  @Override
  public boolean edit(ExpeStep expeStep) {
    Assert.isTrue(this.exist(expeStep.getId()), "实验步骤的ID不存在");
    Assert.notNull(experimentMapper.selectById(expeStep.getExpeId()), "实验信息的ID不存在");
    int i = expeStepMapper.updateById(expeStep);
    Assert.isTrue(i > 0, "实验步骤修改失败");
    return true;
  }

  @Override
  public boolean delete(int id) {
    Assert.isTrue(this.exist(id), "实验步骤的ID不存在");
    int i = expeStepMapper.deleteById(id);
    Assert.isTrue(i>0, "实验步骤删除失败");
    return true;
  }

  @Override
  public MyPage select(int pageSize, int pageNum) {
    List<ExpeStep> expeSteps = expeStepMapper.selectByPage(pageSize, (pageNum - 1) * pageSize);
    return MyPage.buildPage(pageSize, pageNum, this.count(), expeSteps);
  }

  @Override
  public int deleteByExprId(int expeId) {
    return this.expeStepMapper.deleteByExprId(expeId);
  }

  private boolean exist(int id) {
    ExpeStep expeStep = this.getById(id);
    Assert.notNull(expeStep, "无id对应的实验信息");
    return true;
  }

}
